package com.ys.util;

import java.math.BigDecimal;
import java.math.BigInteger;

public class EthUtil {
        public static final int PASS_RADIX = 16;//eth 数字都是16进制
        public static final double FEE = 0.000000000000021000;
        static final double PASS_VALUE = 1.0E18; //eth 单位wei 转成 币

        public static double hexToDouble(String hex) {
            if (hex == null || hex.length() < 2) {
                return 0;
            }
            hex = hex.toLowerCase();
            if (!hex.startsWith("0x")) {
                return 0;
            }
            final String longStr = hex.substring(2);
            final BigInteger in = new BigInteger(longStr, PASS_RADIX);
            return DoubleUtil.round(DoubleUtil.divide(in.doubleValue(), PASS_VALUE, 10), 8);
        }


        public static double hexToDouble(String hex,double PASS_VALUE) {
            if (hex == null || hex.length() < 2) {
                return 0;
            }
            hex = hex.toLowerCase();
            if (!hex.startsWith("0x")) {
                return 0;
            }
            final String longStr = hex.substring(2);
            final BigInteger in = new BigInteger(longStr, PASS_RADIX);
            return DoubleUtil.round(DoubleUtil.divide(in.doubleValue(), PASS_VALUE, 10), 8);
        }


        public static BigInteger hexToBigInteger(String hex) {
            if (hex == null || hex.length() < 2) {
                return new BigInteger( "0" );
            }
            hex = hex.toLowerCase();
            if (!hex.startsWith("0x")) {
                return new BigInteger( "0" );
            }
            final String longStr = hex.substring(2);
            return new BigInteger(longStr, PASS_RADIX);
        }

        public static long hexToLong(String hex) {
            if (hex == null || hex.length() < 2) {
                return 0;
            }
            hex = hex.toLowerCase();
            if (!hex.startsWith("0x")) {
                return 0;
            }
            final String longStr = hex.substring(2);
            return Long.parseLong(longStr, PASS_RADIX);
        }

    public static Integer hexToInt(String hex) {
        if (hex == null || hex.length() < 2) {
            return 0;
        }
        hex = hex.toLowerCase();
        if (!hex.startsWith("0x")) {
            return 0;
        }
        final String longStr = hex.substring(2);
        return Integer.parseInt(longStr, PASS_RADIX);
    }


    public static String longToHex(final long value) {
            return "0x" + Long.toHexString(value);
        }

        public static String doubleToHex(final double value) {
            // 转成 hex
            final BigDecimal bd1 = new BigDecimal(Double.toString(value));
            final BigDecimal bd2 = new BigDecimal(Double.toString(PASS_VALUE));
            final BigInteger a = bd1.multiply(bd2).toBigInteger();
            return "0x" + a.toString(PASS_RADIX);
        }
    public static BigDecimal double18(final double value) {
        // 转成 hex
        final BigDecimal bd1 = new BigDecimal(Double.toString(value));
        final BigDecimal bd2 = new BigDecimal(Double.toString(PASS_VALUE));
        return bd1.multiply(bd2);

    }




}
